#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int maxFreeTime(int eventTime, int k, vector<int>& startTime, vector<int>& endTime) {
        vector<int> leisure;
        int start = 0;
        for (int i = 0; i < startTime.size(); i++)
        {
            leisure.push_back(startTime[i] - start);
            start = endTime[i];
        }
        leisure.push_back(eventTime - start);
        int left = 0, sum = 0, res = 0;
        for (int right = 0; right < leisure.size(); right++)
        {
            sum += leisure[right];
            if (right - left > k)
            {
                sum -= leisure[left++];
            }
            res = max(res, sum);
        }
        return res;
    }
};